//
//  StatusCell.swift
//  MyWeibo
//
//  Created by 余丽丽 on 15/10/13.
//  Copyright © 2015年 余丽丽. All rights reserved.
//

import UIKit
let statusCellControlMargin: CGFloat = 8.0
// 微博 Cell 的标示符枚举
enum StatusCellIdentifier: String {
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    //静态函数
    static func cellID(status : Status) -> String{
    
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
    }
}

class StatusCell: UITableViewCell {

    var status:Status?{
        didSet{

            if let url = status?.user?.imageURL{
                iconView.sd_setImageWithURL(url)
            }
            
            nameLabel.text = status?.user?.name ?? ""
            timeLabel.text = NSDate.sinaDate(status?.created_at ?? "")?.dateDesctiption
            sourceLabel.text = status?.source
            
            memberIconView.image = status?.user?.memberImage
            vipIconView.image = status?.user?.VIPImage
            contentLabel.text = status?.text ?? ""
            
            //TODO:|添加pictureView自动计算的宽高
            pictureView.status = status?.retweeted_status ?? status
            pictureHeightConstraint?.constant = pictureView.bounds.size.height
            pictureWidthConstraint?.constant = pictureView.bounds.size.width
            pictureTopConstraint?.constant = (pictureView.bounds.size.height == 0) ? 0 : statusCellControlMargin
            
            let name = status?.retweeted_status?.user?.name ?? ""
            let text = status?.retweeted_status?.text ?? ""
            forwardContentLabel.text = "@" + name + text
            
            
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
       
        // Initialization code
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI(reuseIdentifier!)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    func setupUI(reuseIdentifier:String){
        
        contentView.addSubview(iconView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(sourceLabel)
        contentView.addSubview(memberIconView)
        contentView.addSubview(vipIconView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        if reuseIdentifier == StatusCellIdentifier.ForwardCell.rawValue {
            
            // A insertSubView B belowSubview:C  是将B插入A并且在A已有的子视图C的下面
            // A insertSubView B aboveSubview:C  是将B插入A并且在A已有的子视图C的上面
            contentView.insertSubview(backButton, belowSubview: pictureView)
            contentView.insertSubview(forwardContentLabel, aboveSubview: backButton)
            
        }
        
        // 布局
        //  头像
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: 35, height: 35), offset: CGPoint(x: statusCellControlMargin, y: statusCellControlMargin))
        // 名字标签
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: 2 * statusCellControlMargin, y: 0))
        // 发布时间标签
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: 2 * statusCellControlMargin, y: 0))
        // 来源标签
        sourceLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: timeLabel, size: nil, offset: CGPoint(x: statusCellControlMargin, y: 0))
        // 会员图标
        memberIconView.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: nameLabel, size: nil, offset: CGPoint(x: statusCellControlMargin, y: 0))
        // vip 图标
        vipIconView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: statusCellControlMargin, y: statusCellControlMargin))
        // 文字内容标签
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: iconView, size: nil, offset: CGPoint(x: 0, y: statusCellControlMargin))
        // 文字宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * statusCellControlMargin))

        var con: [NSLayoutConstraint]
        
        if reuseIdentifier == StatusCellIdentifier.ForwardCell.rawValue {
            // 转发
            backButton.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: nil, offset: CGPoint(x: -statusCellControlMargin, y: statusCellControlMargin))
            
            contentView.addConstraint(NSLayoutConstraint(item: backButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
            
            forwardContentLabel.ff_AlignInner(type: ff_AlignType.TopLeft, referView: backButton, size: nil, offset: CGPoint(x: statusCellControlMargin, y: statusCellControlMargin))
            
            // 配图
            pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: forwardContentLabel, size: nil, offset: CGPoint(x: 0, y: statusCellControlMargin))
            con = pictureView.ff_AlignInner(type: ff_AlignType.BottomLeft, referView: backButton, size: CGSize(width: 100, height: 100), offset: CGPoint(x: statusCellControlMargin, y: -statusCellControlMargin))
            
            bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: backButton, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: 0, y: 0))
            
        }else{
           
        con = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 200, height: 200), offset: CGPoint(x: 0, y: statusCellControlMargin))
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -statusCellControlMargin, y: statusCellControlMargin))
        
        }
        
        pictureWidthConstraint = pictureView.ff_Constraint(con, attribute: NSLayoutAttribute.Width)
        pictureHeightConstraint = pictureView.ff_Constraint(con, attribute: NSLayoutAttribute.Height)
        pictureTopConstraint = pictureView.ff_Constraint(con, attribute: NSLayoutAttribute.Top)
        
        // 底部约束
        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
        
    }
    // MARK:约束
     var pictureWidthConstraint : NSLayoutConstraint?
     var pictureHeightConstraint : NSLayoutConstraint?
     var pictureTopConstraint : NSLayoutConstraint?
    
    // MARK : -懒加载控件
    // 1. 头像图标
     lazy var iconView: UIImageView = UIImageView()
     // 2. 姓名
     lazy var nameLabel: UILabel = UILabel(color: UIColor.orangeColor(), fontSize: 14)
    // 3. 时间标签
     lazy var timeLabel: UILabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 9)
    // 4. 来源标签
     lazy var sourceLabel: UILabel = UILabel(color: UIColor.lightGrayColor(), fontSize: 9)
    // 5. 会员图标
     lazy var memberIconView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    // 6. vip图标
     lazy var vipIconView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    // 7.内容标签
     lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.blackColor(), fontSize: 15)
        label.numberOfLines = 0
        
        
        return label
        }()
    // 8.配图视图
    // TODO:|返回StatusPictureView
    lazy var pictureView: StatusPictureView = StatusPictureView()
    
//    lazy var pictureView: UIView = {
//        let view = UIView()
//        view.backgroundColor = UIColor.orangeColor()
//        return view
//        
//        }()
    
    // 9.转发
    // 转发微博的文字内容
    private lazy var forwardContentLabel : UILabel = {
        let label = UILabel(color: UIColor.grayColor(), fontSize: 15)
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * statusCellControlMargin
        label.numberOfLines = 0
        return label
        }()
    
    //转发微博的背景按钮 为了以后点击可以跳转到原微博
    private lazy var backButton: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        
        return btn
        }()



    // 9.底部视图
    lazy var bottomView: StatusBottomView = StatusBottomView()
    
    
    
}
